package com.tanhua.sso.service;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.tanhua.sso.config.HuanXinConfig;
import com.tanhua.sso.vo.HuanXinUser;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.io.IOException;
import java.util.*;

/**
 * 环信服务
 */
@Service
public class HuanXinService {

    @Autowired
    private HuanXinConfig huanXinConfig;
    @Autowired
    private RestTemplate restTemplate;
    @Autowired
    private HuanXinTokenService huanXinTokenService;

    private static final ObjectMapper MAPPER = new ObjectMapper();

    /**
     * 注册环信用户
     */
    public boolean register(Long userId) throws Exception {

        String url = this.huanXinConfig.getUrl() + this.huanXinConfig.getOrgName() + "/" + this.huanXinConfig.getAppName() + "/users";

        String token = this.huanXinTokenService.getToken();

        //请求头信息
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.add("Content-Type", "application/json");
        httpHeaders.add("Authorization", "Bearer " + token);

        List<HuanXinUser> huanXinUsers = new ArrayList<>();
        HuanXinUser huanXinUser = new HuanXinUser(userId.toString(), DigestUtils.md5Hex(userId + "_itcast_tanhua"));
        huanXinUsers.add(huanXinUser);

        HttpEntity<String> httpEntity = new HttpEntity<String>(MAPPER.writeValueAsString(huanXinUsers), httpHeaders);
        ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(url, httpEntity, String.class);
        return responseEntity.getStatusCodeValue() == 200;
    }

    /**
     * 添加环信好友
     */
    public boolean contactUsers(Long userId, Long friendId) throws IOException {
        String targetUrl = this.huanXinConfig.getUrl() + this.huanXinConfig.getOrgName() + "/" + this.huanXinConfig.getAppName() + "/users/" + userId + "/contacts/users/" + friendId;
        String token = this.huanXinTokenService.getToken();//http://a1.easemob.com/1106201018153926/tanhua/users/101/contacts/users/1
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Content-Type", "application/json");
        headers.add("Authorization", "Bearer " + token);

        HttpEntity<String> httpEntity = new HttpEntity<String>(headers);
        ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(targetUrl, httpEntity, String.class);
        return responseEntity.getStatusCodeValue() == 200;
    }

    /**
     * 发送环信消息
     */
    public boolean sendMsg(String target, String type, String msg) throws Exception {
        String targetUrl = this.huanXinConfig.getUrl() + this.huanXinConfig.getOrgName() + "/" + this.huanXinConfig.getAppName() + "/messages";
        String token = this.huanXinTokenService.getToken();
        // 请求头
        HttpHeaders headers = new HttpHeaders();
        headers.add("Authorization", "Bearer " + token);

        Map<String, Object> paramMap = new HashMap<>();

        Map<String, Object> msgMap = new HashMap<>();
        msgMap.put("type", type);
        msgMap.put("msg", msg);

        paramMap.put("msg", msgMap);
        paramMap.put("target_type", "users");
        paramMap.put("target", Arrays.asList(target));
        //表示消息发送者;无此字段Server会默认设置为“from”:“admin”，有from字段但值为空串(“”)时请求失败
        //msgMap.put("from", type);
        HttpEntity<String> httpEntity = new HttpEntity<String>(MAPPER.writeValueAsString(paramMap), headers);
        ResponseEntity<String> responseEntity = this.restTemplate.postForEntity(targetUrl, httpEntity, String.class);
        return responseEntity.getStatusCodeValue() == 200;
    }
}
